﻿
//计算分页的显示
function FillPager(selector, config) {

    var linkCount = 4; //显示的分页链接个数

    var pager = $(selector);
    if (pager.length == 0) {
        return;
    }
    if (config == null || config.TotalCount == null) {
        throw new Error("参数不足！");
    }
    if (parseInt(config.TotalCount) < 0) {
        throw new Error("TotalCount参数错误！");
    }
    if (config.Click && config.Click.constructor !== Function) {
        throw new Error("请设置回调参数Click！");
    }
    config.PageNumber = Math.max(1, parseInt(config.PageNumber) || 1);
    config.PageSize = parseInt(config.PageSize) || 10;
    if (config.PageSize < 1) {
        config.PageNumber = 10;
    }

    pager.empty();

    var totalCount = config.TotalCount; //总记录数
    var totalPageCount = Math.ceil(config.TotalCount / config.PageSize); //总分页数

    if (false) { }
    else {
        if (config.Type == 1) {
            var ul = $('<ul class="page" style="float: right;height:31px;"></ul>');
            var end = totalPageCount;
            var btns = [];
            //btns.push(1);
            //btns.push("...");
            //btns.push(3);
            //btns.push(4);
            //btns.push(5);
            //btns.push(6);
            //btns.push(7);
            //btns.push("...");
            //btns.push(11);

            if (config.PageNumber == 1) {
            } else {
                ul.append($('<li class="page_off page_left" ></li>').addClass("page_hover").attr("gotopage", config.PageNumber - 1));
            }
            if (config.PageNumber >= 1 && end > 6) {
                if (config.PageNumber <= 3 && config.PageNumber <= end - 3) {
                    btns.push(1);
                    btns.push(2);
                    btns.push(3);
                    btns.push(4);
                    btns.push(5);
                    btns.push("...");
                    btns.push(end);//最后一页```````

                }
                if (config.PageNumber >= 4 && config.PageNumber <= end - 4) {
                    btns.push(1);
                    //  btns.push("...");
                    for (var i = -1; i < 3; i++) {
                        btns.push(config.PageNumber + i);
                    }
                    btns.push("...");
                    btns.push(end);//最后一页```````
                }
                if (config.PageNumber > end - 4 && config.PageNumber <= end) {
                    btns.push(1);
                    btns.push("...");
                    btns.push(end - 4);
                    btns.push(end - 3);
                    btns.push(end - 2);
                    btns.push(end - 1);
                    btns.push(end);
                }
            } else {
                for (var i = 1; i <= end; i++) {
                    btns.push(i);
                }
            }
            for (var i in btns) {
                var txt = btns[i];
                switch (txt) {
                    case "...": ul.append($('<li class="page_off">…</li>').css({ "background": "#e6e6e6" }));
                    default:
                        if (txt == config.PageNumber) {
                            ul.append($('<li class="page_on"></li>').addClass("page_hover").text(config.PageNumber));
                        } else if (txt != "...") {
                            ul.append($('<li class="page_off"></li>').addClass("page_hover").text(txt).attr("gotopage", txt));
                        }
                }

            }
            if (config.PageNumber < end) {
                ul.append($('<li class="page_off page_right"></li>').addClass("page_hover").attr("gotopage", config.PageNumber + 1));
            }

            ul.find("li[gotopage]").click(function () {
                var val = parseInt($(this).attr("gotopage"));
                if (isNaN(val) == false && val != config.PageNumber) {
                    config.Click(val);
                }
            });

            //var ul = $('<ul class="page" style="float: right;"></ul>');
            //var begin10 = parseInt((config.PageNumber - 1) / linkCount) * linkCount + 1;
            //var end10 = totalPageCount;
            //if (config.PageNumber == 1) {
            //    // ul.append($('<li class="page_off" ><img src="images/page_left.png" /></li>').addClass("page_hover"));
            //} else {
            //    ul.append($('<li class="page_off page_left" ></li>').addClass("page_hover").attr("gotopage", config.PageNumber - 1));
            //}
            //if (config.PageNumber > linkCount) {
            //    ul.append($('<li class="page_off"></li>').addClass("page_hover").text(1).attr("gotopage", 1));
            //}
            //if (totalPageCount - linkCount > begin10) end10 = begin10 + linkCount;
            //for (var i = begin10 ; i < end10; i++) {
            //    var li = $('<li />').text(i.toString());
            //    if (config.PageNumber == i) {
            //        li.addClass("page_on").addClass("page_hover");
            //    } else {
            //        li.addClass("page_off").addClass("page_hover").attr("gotopage", i.toString());
            //    }
            //    ul.append(li);
            //}        
            //if (end10 != totalPageCount) {
            //    ul.append($('<li class="page_off">…</li>').css({ "background": "#e6e6e6" }));
            //    ul.append($('<li class="page_off"></li>').addClass("page_hover").text(totalPageCount).attr("gotopage", totalPageCount));
            //} else if (end10 == config.PageNumber) {
            //    ul.append($('<li class="page_on"></li>').addClass("page_hover").text(end10));
            //} else {
            //    ul.append($('<li class="page_off"></li>').addClass("page_hover").text(end10).attr("gotopage", end10));
            //}
            ////
            //if (config.PageNumber == totalPageCount) {
            //    ul.append($('<li class="page_off page_right"></li>').addClass("page_hover"));
            //} else {
            //    ul.append($('<li class="page_off page_right"></li>').addClass("page_hover").attr("gotopage", config.PageNumber + 1));
            //}

            ul.append($('<li class="page_ic" style="width:78px;">转到 <input name="" type="text" class="txtPageGo form-control" style="display:inline-block;"/></li>')).find(".txtPageGo").val(config.PageNumber);
            ul.find(".txtPageGo").keyup(function () {
                this.value = this.value.replace(/[^\d]/g, "");
                var num = $(this).val();
                if (+num > end) {
                    $(this).val(end);
                }
            });
            //ul.find(".txtPageGo").bind('blur', function () {
            //    this.value = this.value.replace(/[^\d]/g, "");
            //    console.log(44)
            //    var num = $(this).val();
            //    if (+num > end) {
            //        $(this).val(end);
               
            //    }
            //});
            ul.find(".txtPageGo").keypress(function (e) {
                var ev = document.all ? window.event : e;
                if (ev.keyCode == 13) {
                    if (+$(this).val() <= 0) {
                        $(this).val("1");
                    }
                   //onsole.log($(this).val());
                    config.Click($(this).val());
                    $(this).blur();
                }
            });
            

            ul.append($('<li class="page_last page_off ">确定</li>').click(function () {
                var val = parseInt(ul.find(".txtPageGo").val());
                if (isNaN(val) == false && val != config.PageNumber) {
                    if (+val <= 0) {
                        ul.find(".txtPageGo").val("1");
                        val = 1;
                    }
                    config.Click(val);
                }
            }));
            ul.append($('<li class="page_ic" style="width:auto;">每页记录数 <select name="selectedPageSize" id="selectedPageSize" style="line-height:29px;height:29px;">' +
                                '<option value="10">10</option>' +
                                '<option value="20">20</option>' +
                                '<option value="30">30</option>' +
                                '<option value="50">50</option>' +
                                '<option value="100">100</option>' +
                            '</select></li>'));
            var selectPageSize = ul.find("select[name=selectedPageSize]");
            selectPageSize.val(config.PageSize);
            selectPageSize.change(function () {
                config.Click(1);
                $(window).resize();
            });

            ////ul.append($('<li class="page_number"><span id="spanPageMessage">共' + totalCount + '条/分' + totalPageCount + '页/每页' + config.PageSize + '条</span></li>'));
            //ul.find("li[gotopage]").click(function () {
            //    var val = parseInt($(this).attr("gotopage"));
            //    if (isNaN(val) == false && val != config.PageNumber) {
            //        config.Click(val);
            //    }
            //});

        } else if (config.Type == 2) {
            var ul = $('<div class="bton_left_nav" style="margin-left: 20px;">');
            if (config.PageNumber == 1) {
                ul.append($('<a href="javascript:void(0)" class="btn_left last"></a>'));
            } else {
                ul.append($('<a href="javascript:void(0)" class="btn_left last"></a>').attr("gotopage", config.PageNumber - 1));
            }
            ul.append($('<span class="num">' + config.PageNumber + '</span>'));
            ul.append($('<span class="nu">/</span>'));
            ul.append($('<span class="num">' + totalPageCount + '</span>'));
            if (config.PageNumber == totalPageCount||totalCount==0) {
                ul.append($('<a href="javascript:void(0)" class="btn_right last"></a>'));
            } else {
                ul.append($('<a href="javascript:void(0)" class="btn_right last"></a>').attr("gotopage", config.PageNumber + 1));
            }
            ul.find("[gotopage]").click(function () {
                var val = parseInt($(this).attr("gotopage"));
                if (isNaN(val) == false && val != config.PageNumber) {
                    config.Click(val);
                }
            });

        } else if (config.Type == 3) {
            var ul = $('<ul class="page02" style="float: right;"></ul>');
            var end = totalPageCount;
            var btns = [];
            //btns.push(1);
            //btns.push("...");
            //btns.push(3);
            //btns.push(4);
            //btns.push(5);
            //btns.push(6);
            //btns.push(7);
            //btns.push("...");
            //btns.push(11);

            if (config.PageNumber == 1) {
            } else {
                ul.append($('<li class="page_off page_left" ></li>').addClass("page_hover").attr("gotopage", config.PageNumber - 1));
            }
            if (config.PageNumber >= 1 && end > 6) {
                if (config.PageNumber <= 3 && config.PageNumber <= end - 3) {
                    btns.push(1);
                    btns.push(2);
                    btns.push(3);
                    btns.push(4);
                    btns.push(5);
                    btns.push("...");
                    btns.push(end);

                }
                if (config.PageNumber >= 4 && config.PageNumber <= end - 4) {
                    btns.push(1);
                    //  btns.push("...");
                    for (var i = -1; i < 3; i++) {
                        btns.push(config.PageNumber + i);
                    }
                    btns.push("...");
                    btns.push(end);
                }
                if (config.PageNumber > end - 4 && config.PageNumber <= end) {
                    btns.push(1);
                    btns.push("...");
                    btns.push(end - 4);
                    btns.push(end - 3);
                    btns.push(end - 2);
                    btns.push(end - 1);
                    btns.push(end);
                }
            } else {
                for (var i = 1; i <= end; i++) {
                    btns.push(i);
                }
            }
            for (var i in btns) {
                var txt = btns[i];
                switch (txt) {
                    case "...": ul.append($('<li class="page_off">…</li>').css({ "background": "#e6e6e6" }));
                    default:
                        if (txt == config.PageNumber) {
                            ul.append($('<li class="page_on"></li>').addClass("page_hover").text(config.PageNumber));
                        } else if (txt != "...") {
                            ul.append($('<li class="page_off"></li>').addClass("page_hover").text(txt).attr("gotopage", txt));
                        }
                }

            }
            if (config.PageNumber < end) {
                ul.append($('<li class="page_off page_right"></li>').addClass("page_hover").attr("gotopage", config.PageNumber + 1));
            }

            ul.find("li[gotopage]").click(function () {
                var val = parseInt($(this).attr("gotopage"));
                if (isNaN(val) == false && val != config.PageNumber) {
                    config.Click(val);
                }
            });
        }
        pager.append(ul);
    }
}